Install the redis cluster on redhat6.4 [tutorial]

  • 2020-05-12 06:25:33
  • OfStack

Reference:

http: / / redis io/topics/cluster - tutorial (mainly Creating a Redis Cluster using the create - cluster script part)

https: / / ruby. taobao. org /

Before installing a piece of unfamiliar software, please check INSTALL and README. This is a habit. In production, you should establish common users and adjust appropriate parameters.

Download unzip and install redis

make test prompts for a higher version of tcl, skip to any problems you may encounter during the installation process


wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar xf redis-3.0.7.tar.gz 
cd redis-3.0.7
mkdir -p /opt/redis
make test
make PREFIX=/opt/redis install

Copy the two scripts to the installed directory


 cp ~/redis-3.0.7/src/redis-trib.rb /opt/redis/

  cp ~/redis-3.0.7/utils/create-cluster/create-cluster /opt/redis/1212

 

According to the actual modification /opt/redis/ create-cluster. There are several changes

3 variables BASEDIR,BINDIR and DATADIR have been added.

b. Modify the relevant command path,

Before c.start, enter DATADIR,start and return to the original directory

Before d.clean, enter DATADIR,start and return to the original directory

e.create's host has been changed from 127.0.0.1 to 192.168.1.194(Too many Cluster redirections is sometimes quoted as Too many Cluster redirections)

Below is the revised shell


 #!/bin/bash

  # Settings

  PORT=30000

  TIMEOUT=2000

  NODES=6

  REPLICAS=1

  BASEDIR=/opt/redis

  BINDIR=$BASEDIR/bin

  DATADIR=$BASEDIR/data

 
  # You may want to put the above config parameters into config.sh in order to

  # override the defaults without modifying this script.

  if [ -a config.sh ]

  then

  source "config.sh"

  fi

  # Computed vars

  ENDPORT=$((PORT+NODES))

  if [ "$1" == "start" ]

  then

  cd $DATADIR

  while [ $((PORT < ENDPORT)) != "0" ]; do

  PORT=$((PORT+1))

  echo "Starting $PORT"

  $BINDIR/redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes

  done

  cd -

  exit 0

  fi

  if [ "$1" == "create" ]

  then

  HOSTS=""

  while [ $((PORT < ENDPORT)) != "0" ]; do

  PORT=$((PORT+1))

  HOSTS="$HOSTS 192.168.1.194:$PORT"

  done

  $BASEDIR/redis-trib.rb create --replicas $REPLICAS $HOSTS

  exit 0

  fi

  if [ "$1" == "stop" ]

  then

  while [ $((PORT < ENDPORT)) != "0" ]; do

  PORT=$((PORT+1))

  echo "Stopping $PORT"

  $BINDIR/redis-cli -p $PORT shutdown nosave

  done

  exit 0

  fi

  if [ "$1" == "watch" ]

  then

  PORT=$((PORT+1))

  while [ 1 ]; do

  clear

  date

  $BINDIR/redis-cli -p $PORT cluster nodes | head -30

  sleep 1

  done

  exit 0

  fi

  if [ "$1" == "tail" ]

  then

  INSTANCE=$2

  PORT=$((PORT+INSTANCE))

  tail -f ${PORT}.log

  exit 0

  fi

  if [ "$1" == "call" ]

  then

  while [ $((PORT < ENDPORT)) != "0" ]; do

  PORT=$((PORT+1))

  $BINDIR/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9

  done

  exit 0

  fi

  if [ "$1" == "clean" ]

  then

  cd $DATADIR

  rm -rf *.log

  rm -rf appendonly*.aof

  rm -rf dump*.rdb

  rm -rf nodes*.conf

  cd -

  exit 0

  fi

  echo "Usage: $0 [start|create|stop|watch|tail|clean]"

  echo "start -- Launch Redis Cluster instances."

  echo "create -- Create a cluster using redis-trib create."

  echo "stop -- Stop Redis Cluster instances."

  echo "watch -- Show CLUSTER NODES output (first 30 lines) of first node."

  echo "tail -- Run tail -f of instance at base port + ID."

  echo "clean -- Remove all instances data, logs, configs."123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102

Don't forget to create the data directory mkdir-p /opt/redis/data

Start and stop the cluster according to the above reference

To start the cluster, type in /opt/redis/ create-cluster start enter, then type in /opt/redis/ create-cluster create enter, then enter yes enter

To stop the cluster: type in /opt/redis/ create-cluster stop enter

If it has been started before and the data is not 1, an error will be reported when create, but first /opt/redis/ create-cluster clean

test


<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.8.1</version>
</dependency>

The statement JedisCluster Bean


@Bean
 public JedisCluster jedisCluster(){
  Set<HostAndPort> nodes=new HashSet<>(3);
  nodes.add(new HostAndPort("192.168.1.194",30001));
  nodes.add(new HostAndPort("192.168.1.194",30002));
  nodes.add(new HostAndPort("192.168.1.194",30003));
  return new JedisCluster(nodes,2000,5);
 }

Test set and get


 AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(AppConfig.class);
  JedisCluster jedisCluster = (JedisCluster) context.getBean("jedisCluster");
  jedisCluster.set("xxx","123");
  System.out.println("jedisCluster.get = " + jedisCluster.get("xxx"));

Problems you may encounter during installation:

make test, remind You need tcl 8.5 or newer in order to run the Redis test. To http: / / www tcl. tk/software/tcltk/download. Download Tcl html,


wget http://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.gz
tar xf tcl8.5.19-src.tar.gz
cd tcl8.5.19/unix
./configure
make
make test
make install

Because create-cluster create will call redis-trib.rb, which is an ruby script, you are prompted to install yum install-y ruby if ruby is not installed

If you are prompted to load rubygems incorrectly, use the following method to install rubygems

a. https: / / rubygems org pages/download installation package download tgz format (wget may not, in windows by a whirlwind, or thunderbolt download)

b.mount-t cifs-o username=xiejx618,password=123456 //192.168.1.115/share /share


cp /share/rubygems-2.6.4.tgz ./
tar xf rubygems-2.6.4.tgz
cd rubygems-2.6.4
ruby setup.rb

If you are prompted to use no such file load rdoc/rdoc, install yum install-y rdoc

If you are prompted with no such file load redis, gem install redis-v 3.0.7

gem can not use the default source because of the wall, so it is modified to taobao source

A few commands you might use

Help :gem sources --help

See source :gem sources-l

Delete the source: gem sources - r https: / / rubygems org /

Add source: gem sources - a https: / / ruby taobao. org /

Update source cache :gem sources-u


Related articles: